package org.hzero.business.halm.dto;


import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: Attribute
 * @Description: 存放attribute字段的数据结构
 * @author: tianhao.luo@hand-china.com
 * @date: 2020/11/19  9:34
 */
public class Attribute {
    private static final String REX="&@&";

    private String curr;
    private String target;

    private List<Attribute> attributeList;

    public Attribute() {
    }

    public Attribute(String curr, String target) {
        this.curr = curr;
        this.target = target;
        attributeList=parser();
    }

    /**
     * 判断该attribute字段得curr和target是否发生变化
     * @return 变化了返回true
     */
    public Boolean isChange(){
        //else if表示，上面的if判断不走，才走这个else if。而两个分开的if表示都要走
       return !Objects.equals(this.curr,this.target);
    }

    public List<Attribute> parser(){
        if (CollectionUtils.isEmpty(attributeList)){
            return new ArrayList<>();
        }
        String[] currList = curr.split(REX);
        String[] targetList = target.split(REX);
        if (currList.length!=targetList.length){
            //错误数据
        }
        for (int i = 0; i < currList.length; i++) {
            attributeList.add(new Attribute(currList[i],targetList[i]));
        }
        return attributeList;
    }
    public Attribute getAttribute(Integer index){
        if (CollectionUtils.isEmpty(attributeList)){
            return null;
        }
        if (index>attributeList.size()){
            return null;
        }
        return attributeList.get(index);
    }

    public String getTargetTwoParam(){
        return target.split(REX)[1];
    }

    public String getCurrTwoParam(){
        return curr.split(REX)[1];
    }

    public String getTargetOneParam(){
        return target.split(REX)[0];
    }

    public String getCurrOneParam(){
        return curr.split(REX)[0];
    }

    public String getCurr() {
        return curr;
    }

    public void setCurr(String curr) {
        this.curr = curr;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }
}
